package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.ExamNoticeEntity;
import com.tfjybj.itoo.exam.model.ExamNoticeModel;
import com.tfjybj.itoo.exam.provider.service.ExamNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * ExamNoticeController
 * examNotice表
 *
 * @author 贾建清
 * @version 2.0.0
 * @since 2.0.0 2018-12-26 20:56:50
 */
@Api(tags = {"examNotice表接口"})
@RequestMapping(value = "/examNotice")
@RestController
@Slf4j
public class ExamNoticeController {

    @Resource
    private ExamNoticeService examNoticeService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model ExamNoticeModel
     * @return 添加的结果
     * @author 贾建清
     * @since 2.0.0 2018-12-26 20:56:50
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody ExamNoticeModel model) {
        ExamNoticeEntity examNoticeEntity = new ExamNoticeEntity();
        BeanUtils.copyProperties(model, examNoticeEntity);
        examNoticeService.save(examNoticeEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 贾建清
     * @since 2.0.0 2018-12-26 20:56:50
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        examNoticeService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 贾建清
     * @since 2.0.0 2018-12-26 20:56:50
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        examNoticeService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model ExamNoticeModel
     * @return 修改后的结果
     * @author 贾建清
     * @since 2.0.0 2018-12-26 20:56:50
     */
    @ApiOperation(value = "根据id修改examNotice")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody ExamNoticeModel model) {
        ExamNoticeEntity examNoticeEntity = new ExamNoticeEntity();
        BeanUtils.copyProperties(model, examNoticeEntity);
        examNoticeService.updateById(examNoticeEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找ExamNotice
     *
     * @param id 主键id
     * @return 根据id查找的结果
     * @author 贾建清
     * @since 2.0.0 2018-12-26 20:56:50
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        ExamNoticeEntity examNoticeEntity = examNoticeService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examNoticeEntity);
    }

    /**
     * 分页查询所有ExamNotice
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 分页查询的结果
     * @author 贾建清
     * @since 2.0.0 2018-12-26 20:56:50
     */
    @ApiOperation(value = "分页查询所有ExamNotice")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<ExamNoticeEntity> examNotices = examNoticeService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examNotices);
    }

    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 导入学生考试信息
     *
     * @param multipartFile 文件
     * @param request       要求
     * @param response      返回
     * @return ItooResult
     * @author 贾建清
     * @since 2.0.0 2018-12-27 11:48:05
     */
    @ApiOperation(value = "导入学生考试信息", notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importByTemplate"})
    public ItooResult importByTemplate(@RequestParam(value = "file") MultipartFile multipartFile,
                                       HttpServletRequest request, HttpServletResponse response) {
        return examNoticeService.importTemplate(multipartFile, request, response);
    }

    /**
     * 根据errorListId导出未成功导入的信息
     *
     * @param response    响应
     * @param errorListId 为成功导入的分组id
     * @return 导出结果
     * @author 贾建清
     * @since 2.0.0 2018-12-27 21:16:29
     */
    @ApiOperation(value = "根据errorListId导出未成功导入的信息")
    @GetMapping(value = {"/exportErrorById/{errorListId}"})
    public ItooResult exportErrorById(HttpServletResponse response,
                                      @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {
        if (examNoticeService.exportErrorList(errorListId, response)) {
            return ItooResult.build(ItooResult.SUCCESS, "导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "导出失败");
        }
    }

    /**
     * 根据课程搜索人员考试信息
     *
     * @param course
     * @param pageNo
     * @param pageSize
     * @return
     * @author 李娜
     */
    @ApiOperation(value = "根据课程搜索学员考试信息")
    @GetMapping(value = {"/queryNoticeByCourse/{course}/{pageNo}/{pageSize}"})
    public ItooResult queryNoticeByCourse(
            @ApiParam(name = "course", value = "课程名称", required = true) @PathVariable String course,
            @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
            @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<ExamNoticeModel> examNoticeList = examNoticeService.queryNoticeByCourse(course, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examNoticeList);
    }

    /**
     * 高级搜索
     *
     * @param examNoticeModel
     * @param pageNo
     * @param pageSize
     * @return
     * @author 李娜
     */
    @ApiOperation(value = "高级搜索学员考试信息")
    @PostMapping(value = {"/queryNoticeByInfo/{pageNo}/{pageSize}"})
    public ItooResult queryNoticeByInfo(
            @RequestBody ExamNoticeModel examNoticeModel,
            @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
            @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<ExamNoticeModel> examNoticeList = examNoticeService.queryNoticeByInfo(pageNo, pageSize, examNoticeModel);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examNoticeList);
    }


    /**
     * 根据课程导出考生信息
     *
     * @param response 响应
     * @param course   课程
     * @return 是否导出成功
     * @author 贾建清
     * @since 2.0.0 2018-12-28 14:40:26
     */
    @ApiOperation(value = "根据课程导出考生信息")
    @GetMapping(value = {"/exportExamByLikeCourse"})
    public ItooResult exportExamByLikeCourse(HttpServletResponse response,
                                             @RequestParam(required = false, defaultValue = "") String course) {
        if (examNoticeService.exportExamByLikeCourse(course, response)) {
            return ItooResult.build(ItooResult.SUCCESS, "导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "导出失败");
        }
    }


    /**
     * 下载考生考试模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 贾建清
     * @since 2.0.0 2018-12-28 20:38:36
     */
    @ApiOperation(value = "下载考生考试模板，批量导入使用", notes = "下载考生考试模板，批量导入使用")
    @GetMapping(value = {"/downTemplate"})
    public ItooResult downTemplate(HttpServletResponse response) {
        if (examNoticeService.downTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载失败");
        }
    }


    /**
     * 根据学号查询该学生考试信息
     *
     * @param studentNo 学号
     * @return Itoo
     * @author 贾建清
     * @since 2.0.0 2019年1月1日17:20:34
     */
    @ApiOperation(value = "根据学号查询该学生考试信息")
    @GetMapping(value = "/queryExamNoticeByStudentNo/{studentNo}")
    public ItooResult queryExamNoticeByStudentNo(@ApiParam(value = "学号", required = true) @PathVariable String studentNo
    ) {
        List<ExamNoticeModel> examNoticeModelList = examNoticeService.queryExamNoticeByStudentNo(studentNo);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examNoticeModelList);
    }

}    
